<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 95%;overflow-y: hidden">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="客户端标识">
          <a-input placeholder="请输入客户端标识" v-decorator="[ 'clientId', validatorRules.clientId]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="客户端秘钥">
          <a-input placeholder="请输入客户端密钥" v-decorator="[ 'clientSecret', validatorRules.clientSecret]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="域">
          <a-input placeholder="请输入域" v-decorator="[ 'scope', validatorRules.scope]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="授权模式">
          <a-checkbox-group @change="onChange" :defaultValue="model.checkedList">
            <a-checkbox value="password">密码模式</a-checkbox>
            <a-checkbox value="authorization_code">授权码模式</a-checkbox>
            <a-checkbox value="client_credentials">客户端模式</a-checkbox>
            <a-checkbox value="refresh_token">刷新模式</a-checkbox>
          </a-checkbox-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="自动放行">
          <a-radio-group v-model="model.autoapproveValue">
            <a-radio value="true">是</a-radio>
            <a-radio value="false">否</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="令牌失效">
          <a-input-number placeholder="请输入令牌失效时间" :min="0" style="width: 200px" v-model="model.accessTokenValidity"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="刷新时效">
          <a-input-number placeholder="请输入令牌刷新时效" :min="0" style="width: 200px" v-model="model.refreshTokenValidity"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="回调地址">
          <a-input placeholder="请输入回调地址" v-model="model.redirectUri"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="权限">
          <a-input placeholder="请输入权限"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { addClient, updateClient } from '@/api/client'

  export default {
    name: 'ClientModal',
    data () {
      return {
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          clientId: {
            rules: [
              { required: true, message: '请输入客户端标识' }
            ]
          },
          clientSecret: {
            rules: [
              { required: true, message: '请输入客户端密钥' }
            ]
          },
          scope: {
            rules: [
              { required: true, message: '请输入域' }
            ]
          }
        },
        departTree: [],

      }
    },
    created () {

    },
    methods: {
      add () {
        this.edit({})
      },
      edit (record) {
        const that = this
        that.form.resetFields()
        that.visible = true
        that.checkedList = []
        that.model = {}
        if (Object.keys(record).length != 0) {
          that.model = Object.assign({}, record)
          that.model.id = record.clientId
          that.model.autoapproveValue = record.autoapprove
          that.model.refreshTokenValidity = record.refreshTokenValidity
          that.model.accessTokenValidity = record.accessTokenValidity
          that.model.redirectUri = record.webServerRedirectUri
          if (record.authorizedGrantTypes != ''){
            that.model.checkedList = record.authorizedGrantTypes.split(',');
          }
          console.log(this.model)
          //编辑页面禁止修改角色编码
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model, 'clientId', 'clientSecret', 'scope'))
          })
        }
      },
      close () {
        this.$emit('close')
        this.dsType = 0
        this.visible = false
      },
      handleOk () {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            that.model.autoapprove = this.autoapproveValue;
            that.model.accessTokenValidity = this.accessTokenValidity;
            that.model.refreshTokenValidity = this.refreshTokenValidity
            that.model.webServerRedirectUri = this.redirectUri;
            that.model.authorizedGrantTypes = this.checkedList.join(",");
            let formData = Object.assign(this.model, values)
            let obj
            if (!this.model.id) {
              obj = addClient(formData)
            } else {
              obj = updateClient(formData)
            }
            obj.then((res) => {
              console.log(res)
              if (res.code == 200) {
                that.$message.success(res.msg)
                that.$emit('ok')
              } else {
                that.$message.warning(res.msg)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      onChange (checkedList) {
        this.model.checkedList = checkedList;
      }
    }
  }
</script>

<style scoped>

</style>